﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using System.Xml.Linq;
using MyAlbum.Utility;


namespace MyAlbum.Data.Xml
{
    public class SectionRepository :XmlRepository, ISectionRepository
    {
         private static object locker = "locker";
         public SectionRepository(string rootPath)
            : base(rootPath, "Sections")
        {
        }
        #region IRepository<Section> Members
         public int CountItems(bool visible)
         {
             return this.Document.Descendants("Section").Where(a => visible ? (bool)a.Element("Visible") : true).Count();
         }
         public List<MyAlbum.Model.Section> GetItems()
         {
             return GetItems(true);
         }
        public List<MyAlbum.Model.Section> GetItems(bool visible)
        {
            return this.Document.Descendants("Section").Where(s => (visible ? (bool)s.Element("Visible") : true)).Select(s => new MyAlbum.Model.Section()
            {
                Id = s.Attribute("Id").Value,
                Name = s.Element("Name").Value.Trim(),
                Visible = (bool)s.Element("Visible"),
                ContentUrl = s.Element("ContentUrl").Value.Trim(),
                Description = s.Element("Description").Value.Trim()
            }).ToList();
          
        }

        public void AddItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(item.Name);
            item.Id = Guid.NewGuid().ToString();
            lock (locker)
            {
                XElement a = new XElement("Section",
                    new XAttribute("Id", item.Id),
                    new XElement("Visible", item.Visible),                   
                    new XElement("Name", item.Name),
                    new XElement("ContentUrl", item.ContentUrl),
                    new XElement("TimeStamp",item.TimeStamp),
                    new XElement("Description", item.Description));
                Document.Element("Sections").Add(a);
                this.Save();
            }
        }

        public void DeleteItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Section") where e.Attribute("Id").Value == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Remove();
                    this.Save();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Section item)
        {
            Assert.IsNotNull(item);
            Assert.IsNotNull(item.Name);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Section") where e.Attribute("Id").Value == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Element("Visible").ReplaceWith(new XElement("Visible", item.Visible));
                    saved.Element("Name").ReplaceWith(new XElement("Name", item.Name));
                    saved.Element("ContentUrl").ReplaceWith(new XElement("ContentUrl", item.ContentUrl));
                    saved.Element("Description").ReplaceWith(new XElement("Description", item.Description));
                    this.Save();
                }
            }
        }

        #endregion


        #region ISectionRepository Members

        public MyAlbum.Model.Section GetItemById(string sectionId)
        {
            return Document.Descendants("Section")
              .Where(a => a.Attribute("Id").Value == sectionId)
              .Select(
              a => new MyAlbum.Model.Section()
              {
                  Description = a.Element("Description").Value.Trim(),
                  Id = a.Attribute("Id").Value,
                  Name = a.Element("Name").Value.Trim(),
                  TimeStamp = (DateTime)a.Element("TimeStamp"),
                  ContentUrl = a.Element("ContentUrl").Value.Trim(),
                  Visible = (bool)a.Element("Visible")
              }).SingleOrDefault();
          
        }

        #endregion
    }
}
